home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ghostscr / gdevpcfb.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-31  |  6.0 KB  |  217 lines

  1. /* Copyright (C) 1989, 1990 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevpcfb.h */
  21. /* Common parts of IBM PC-compatible display drivers for GhostScript. */
  22. /* This is fundamentally an EGA driver with some parameters */
  23. /* that allow it to drive larger displays. */
  24. /*
  25.  * >>>>>> NOTE: the following must be defined before #include'ing this file:
  26.  * >>>>>>   DEVICE_STRUCT_NAME, DEVICE_NAME, FB_RASTER, SCREEN_HEIGHT,
  27.  * >>>>>>   SCREEN_ASPECT_RATIO, VIDEO_MODE
  28.  */
  29. /* Note that this header file includes executable code, */
  30. /* and assumes an ANSI-compatible compiler. */
  31. #define interrupt            /* patch ANSI incompatibility */
  32. #include <dos.h>
  33. typedef union REGS registers;
  34. #include "gs.h"
  35. #include "gsmatrix.h"            /* for gxdevice.h */
  36. #include "gxbitmap.h"
  37. #include "gxdevice.h"
  38.  
  39. /* Define the short (integer) version of "transparent" color. */
  40. /* ****** Depends on gx_no_color_index being all 1's. ******/
  41. #define no_color ((int)gx_no_color_index)
  42.  
  43. /* For testing, the EGA may be defined as a monochrome, 8-color, or */
  44. /* 16-color device. */
  45. #define ega_bits_of_color 2        /* 0, 1, or 2 */
  46.  
  47. /* Dimensions of screen */
  48. #define screen_size_x (FB_RASTER * 8)
  49. #define screen_size_y SCREEN_HEIGHT
  50. /* Other display parameters */
  51. #define raster_x FB_RASTER
  52. #define aspect_ratio SCREEN_ASPECT_RATIO
  53. #define graphics_video_mode VIDEO_MODE
  54. /* Range of r-g-b values */
  55. #define rgb_max (ega_bits_of_color == 1 ? 1 : 3)
  56.  
  57. /* Typedef for the device structure */
  58. typedef struct gx_device_s gx_device;
  59.  
  60. /* Procedures */
  61.  
  62.     /* See gxdevice.h for the definitions of the procedures. */
  63.  
  64. dev_proc_open_device(ega_open);
  65. dev_proc_close_device(ega_close);
  66. dev_proc_map_rgb_color(ega_map_rgb_color);
  67. dev_proc_map_color_rgb(ega_map_color_rgb);
  68. dev_proc_fill_rectangle(ega_fill_rectangle);
  69. dev_proc_tile_rectangle(ega_tile_rectangle);
  70. int ega_write_dot(P4(gx_device *, int, int, gx_color_index));
  71. dev_proc_copy_mono(ega_copy_mono);
  72. dev_proc_copy_color(ega_copy_color);
  73.  
  74. /* The device descriptor */
  75. private gx_device_procs ega_procs = {
  76.     ega_open,
  77.     gx_default_get_initial_matrix,
  78.     gx_default_sync_output,
  79.     gx_default_output_page,
  80.     ega_close,
  81.     ega_map_rgb_color,
  82.     ega_map_color_rgb,
  83.     ega_fill_rectangle,
  84.     ega_tile_rectangle,
  85.     ega_copy_mono,
  86.     ega_copy_color,
  87.     gx_default_draw_line,
  88.     gx_default_fill_trapezoid,
  89.     gx_default_tile_trapezoid
  90. };
  91. gx_device DEVICE_STRUCT_NAME = {
  92.     sizeof(gx_device),
  93.     &ega_procs,
  94.     DEVICE_NAME,
  95.     screen_size_x, screen_size_y,
  96.     /* The following parameters map an appropriate fraction of */
  97.     /* the screen to an 8.5" x 11" coordinate space. */
  98.     /* This may or may not be what is desired! */
  99.       (screen_size_y * aspect_ratio) / 11.0,    /* x density */
  100.       screen_size_y / 11.0,        /* y density */
  101.     (ega_bits_of_color ? 1 : 0),    /* has_color */
  102.     rgb_max,
  103.     4,            /* bits per color pixel */
  104.     0            /* not opened yet */
  105. };
  106.  
  107. /* Define the color spectrum */
  108. #define black 0
  109. #define blue 1
  110. #define green 2
  111. #define cyan 3
  112. #define red 4
  113. #define magenta 5
  114. #define brown 6
  115. #define white 7
  116. #define dgray 8                /* dark gray is not very usable */
  117. #define lblue 9
  118. #define lgreen 10
  119. #define lcyan 11
  120. #define lred 12
  121. #define lmagenta 13
  122. #define yellow 14
  123. #define bwhite 15
  124.  
  125. /* Forward declarations */
  126. private int ega_get_mode();
  127. private void ega_set_mode(int);
  128.  
  129. /* Save the EGA mode */
  130. private int ega_save_mode = -1;
  131.  
  132. /* Reinitialize the EGA for text mode */
  133. int
  134. ega_close(gx_device *dev)
  135. {    if ( ega_save_mode >= 0 ) ega_set_mode(ega_save_mode);
  136.     return 0;
  137. }
  138.  
  139. /* Map a r-g-b color to an EGA color code. */
  140. /* r, g, b are 0..3. */
  141. private char mono_color[4] =
  142.     { black, dgray, white, bwhite };
  143. gx_color_index
  144. ega_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
  145. {
  146. #ifdef DEBUG
  147.     if ( r > rgb_max || g > rgb_max || b > rgb_max )
  148.        {    eprintf3("Bad rgb values! %ud, %ud, %ud\n", r, g, b);
  149.         exit(1);
  150.        }
  151. #endif
  152. #if ega_bits_of_color == 0
  153.     return (gx_color_index)mono_color[((r * 3) + (g * 6) + b) >> 3];
  154. #else
  155. # if ega_bits_of_color == 1
  156.     return (gx_color_index)
  157.         ((r << 2) + (g << 1) + b +
  158.          ((r | b) << 3));
  159. # else
  160.     return (gx_color_index)
  161.         (((r & 2) << 1) + (g & 2) + ((b & 2) >> 1) +
  162.          (((r & b | g) & 1) << 3));
  163. # endif
  164. #endif
  165. }
  166.  
  167. /* Map a color code to r-g-b.  Surprisingly enough, this is algorithmic. */
  168. int
  169. ega_map_color_rgb(gx_device *dev, gx_color_index color, ushort *prgb)
  170. {
  171. #define icolor (int)color
  172. #if rgb_max > 1
  173.     int aux = (icolor >> 3) & 1;
  174.     prgb[0] = ((icolor >> 1) & 2) + aux;
  175.     prgb[1] = (icolor & 2) + aux;
  176.     prgb[2] = ((icolor << 1) & 2) + aux;
  177. #else
  178.     prgb[0] = icolor >> 2;
  179.     prgb[1] = (icolor >> 1) & 1;
  180.     prgb[2] = icolor & 1;
  181. #endif
  182.     return 0;
  183. #undef icolor
  184. }
  185.  
  186. /* Macro for validating dot parameters x, y, color */
  187. #define validate_dot()\
  188.   if ( (unsigned)x >= (unsigned)(dev->width) ||\
  189.        (unsigned)y >= (unsigned)(dev->height) ||\
  190.        (unsigned)color > 0xf )\
  191.     return -1
  192.  
  193. /* Macro for validating rectangle parameters x, y, w, h */
  194. #define validate_rect()\
  195.   if ( w <= 0 || h <= 0 ) return 0;\
  196.   if ( x < 0 || y < 0 || x + w > dev->width || y + h > dev->height ) return -1
  197.  
  198. /* ------ Internal routines ------ */
  199.  
  200. /* Read the device mode */
  201. private int
  202. ega_get_mode()
  203. {    registers regs;
  204.     regs.h.ah = 0xf;
  205.     int86(0x10, ®s, ®s);
  206.     return regs.h.al;
  207. }
  208.  
  209. /* Set the device mode */
  210. private void
  211. ega_set_mode(int mode)
  212. {    registers regs;
  213.     regs.h.ah = 0;
  214.     regs.h.al = mode;
  215.     int86(0x10, ®s, ®s);
  216. }
  217.